import json
from django.shortcuts import render
from django.views import View
from django.http import JsonResponse
from .models import User
import re
import logging
# Create your views here.

class UserNameView(View):
    """
    用户名是否重复
    """
    def get(self, request, username):
        try:
            count = User.objects.filter(username=username).count()
        except Exception as e:
            logging.info(e)
            return JsonResponse({'code': 400, 'errmsg': '数据库错误'})
        return JsonResponse({'code': 0, 'errmsg': 'ok', 'count': count})

class MobileNameView(View):
    """
    手机号是否重复
    """
    def get(self, request, mobile):
        try:
            count = User.objects.filter(mobile=mobile).count()
        except Exception as e:
            logging.info(e)
            return JsonResponse({'code': 400, 'errmsg': '数据库错误'})
        return JsonResponse({'code': 0, 'errmsg': 'ok', 'count': count})   

from .serializers import LoginSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
# 导入: 
from django.contrib.auth import login


class UserRegisterView(APIView):
    """
    用户注册
    """
    def post(self, request):
        serializer = LoginSerializer(data=request.data)
        if not serializer.is_valid():
            return Response({"code":400, "errmsg":"校验失败"})
        instance = serializer.save()

        # 状态保持
        # request.session['user_id'] = instance.id
        # request.session['is_login'] = True
        login(request, instance)
        return Response({"code": 0,"errmsg": "恭喜，注册成功！"})

    # def post(self, request):
        # 1.获取参数
        # json_dict = json.loads(request.body.decode())
        # username = json_dict.get('username')
        # password = json_dict.get('password')
        # password2 = json_dict.get('password2')
        # mobile = json_dict.get('mobile')
        # sms_code = json_dict.get('sms_code')
        # allow = json_dict.get('allow')
        # # 2.校验参数
        # if not all(['username', 'password', 'password2', 'mobile', 'sms_code', 'allow']):
        #     return JsonResponse({'code': 400, 'errmsg': '缺少参数'})
        # if not re.match(r'^[a-zA-Z0-9_-]{5,20}$', username):
        #     return JsonResponse({'code': 400, 'errmag': '用户名格式错误'})
        # if not re.match(r'^[a-zA-Z0-9]{8,20}$', password):
        #     return JsonResponse({'code': 400, 'errmag': '密码格式错误'})
        # if not re.match(r'^1[3-9]\d{9}$', mobile):
        #     return JsonResponse({'code': 400, 'errmag': '手机号格式错误'})
        # if not password==password2:
        #     return JsonResponse({'code': 400, 'errmsg': '两次密码不一致'})
        # 3.业务数据处理

        # try:
        #     user = User.objects.create_user(
        #         username=username,
        #         password=password,
        #         mobile=mobile
        #     )
        # except Exception as e:
        #     logging.info(e)
        #     return JsonResponse({'code': 400, 'errmsg': '数据库错误'})
        # # 4.构建响应
        # return JsonResponse({'code': 0, 'errmsg': 'ok'})